package com.monkey.tensquare.search.service.impl;
import cn.hutool.core.util.StrUtil;
import com.monkey.tensquare.search.entity.Article;
import com.monkey.tensquare.search.mapper.ArticleMapper;
import com.monkey.tensquare.search.service.ArticleService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import util.IdWorker;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private IdWorker idWorker;
    @Override
    public void add(Article article) {
        article.setId(idWorker.nextId() + "");
        articleMapper.save(article);
    }

    @Override
    public PageResult<Article> search(String kw, Integer page, Integer size) {

        if (StrUtil.isNotBlank(kw)){
            Page<Article> articlePage = articleMapper.findByTitleOrContentLike(kw, kw, PageRequest.of(page - 1, size));
            return new PageResult<>(articlePage.getTotalElements(),articlePage.getContent());
        }
        return new PageResult<>();
    }
}
